package com.aco.party.web.controller.client;

import com.aco.party.util.PathUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import static com.aco.party.util.WebModelUtil.getImgPath;

/**
 * Created by WangShijun on 2016/5/23.
 */
@Controller
public class TestController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping(value = "/upload_test",method = RequestMethod.GET)
    public String uploadVideo(){
        return "client/upload_test";
    }

    @RequestMapping(value = "/upload_test",method = RequestMethod.POST)
    public void uploadVideo(MultipartFile file) throws Exception{
        if (!file.isEmpty()) {
            String name = file.getOriginalFilename();
            String fileSuffix = PathUtil.getExtension(name);
            String parentPath = getImgPath() + "video/";
            OutputStream out = new FileOutputStream(parentPath + name);
            InputStream in = file.getInputStream();
            logger.info("开始保存"  + name);
            int length = 0;
            byte[] buf = new byte[1024];
            while ((length = in.read(buf)) != -1) {
                out.write(buf, 0, length);
            }
            logger.info("结束保存"  + name);
            in.close();
            out.close();
        }
    }
}
